package be.neo4reactome.parser;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class PathwaysParser {
	/**
	 * Reads the pathways file..
	 * @param file Pathways file (tab-delimited)
	 * @return List of pathways.
	 */
	public static Map<String, List<Pathway>> read(String file) {
		
		// Unique mapping protein accession to a list of pathways:
		Map<String, List<Pathway>> pathways = new HashMap<String, List<Pathway>>();

		try {
            BufferedReader reader = new BufferedReader(new FileReader(file));
            String line;
			while ((line = reader.readLine()) != null) {
				String[] proteins = line.split("\\t");
                String protein = "", species ="";
				if (proteins[0].length() > 0) {
					protein = proteins[0];
				}
				
				if (proteins[4].length() > 0) {
					species = proteins[4].trim();
				}
				
                List<Pathway> pathwayList = new ArrayList<Pathway>();
				if (proteins[2].startsWith("[")) {

                    final int offset = proteins[2].indexOf(":") + 1;
                    String processes = proteins[2].substring(offset).trim();
					String[] descriptions = processes.split("; ");
					for (String description : descriptions) {
						pathwayList.add(new Pathway(description.trim(), species));
					}
				}
				
				pathways.put(protein, pathwayList);
			}
			reader.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		return pathways;
	}
	
	public static void main(String[] args) {
		PathwaysParser.read("data/curated_uniprot_2_pathways.txt");
	}

}
